<template>
    <el-container class="el-container">
      <div class="content-box">
        <el-carousel :interval="5000" type="card">
            <el-carousel-item v-for="(image, index) in images" :key="index">
              <el-image :src="image" alt="轮播图"></el-image>
            </el-carousel-item>
        </el-carousel>
        <div class="hot-list">
            <div v-for="(item, index) in hotSearches" :key="index" :class="[ 'hot-content', { 'hot-content-first': index === 0 }, { 'hot-content-second': index === 1 }, { 'hot-content-third': index === 2 } ]">
                <span class="hot-title">
                  <span class="hot-number">{{ index + 1 }}.</span>
                  <a :href="item.link" target="_blank" >{{ item.title }}</a>
                </span>
                <span class="hot-value">热度{{ item.hot }}</span>
            </div>
        </div>
      </div>
    </el-container>
</template>

<script lang="ts" setup>
import { ref, onMounted, watch, computed } from "vue";
import { getNews } from "../api/search";
import { useStore } from "vuex";
import image1 from "@assets/home-image1.png";
import image2 from "@assets/home-image2.png";
import image3 from "../assets/home-image3.png";
const store = useStore();
const images = [image1, image2, image3];
const themeBackColor = computed(() => !store.state.darkTheme ? 'rgba(255,255,255, .1)':'rgba(0,0,0, .1)');
const hotSearches: any = ref([]);
const showHotSearch = async () => {
    try {
        const response = await getNews();
        hotSearches.value = response.data;
    } catch (error) {
        console.error("获取热搜失败：", error);
    }
};

function updateThemeColor() {
    document.documentElement.style.setProperty('--themeBackColor', themeBackColor.value);
}

watch(() => store.state.darkTheme, () => {
    updateThemeColor();
});

onMounted(() => {
    showHotSearch();
    updateThemeColor();
});
</script>

<style lang="scss" scoped>
.content-box {
  width: 95%;
  margin: 0 auto;
}
.el-image {
    height: 100%;
    width: 100%;
}
.el-carousel {
    height: 30vh;
    margin-top: 10px;
}
:deep(.el-carousel__container) {
  height: 100%;
}
.el-carousel__item h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
}
.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n + 1) {
    background-color: #d3dce6;
}
.hot-list {
    height: 45vh;
    margin: 20px auto;
    overflow-y: scroll;

    .hot-content {
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-radius: 5px;
        color: #000;

        .hot-title {
            font-size: 20px;
            .hot-number {
              display: inline-block;
              width: 35px;
            }
            a {
              color: #000;
            }
        }
    }

    .hot-content-first {
        .hot-number, .hot-value {
          color: rgb(245, 55, 55);
        }
    }

    .hot-content-second {
        .hot-number, .hot-value {
          color: rgb(245, 160, 55);
        }
    }

    .hot-content-third {
        .hot-number, .hot-value {
          color: rgb(245, 235, 55);
        }
    }

    .hot-content:hover {
        color: var(--m-color);
        background-color: var(--themeBackColor);
        a {
          color: var(--m-color);
        }
    }

}

.hot-list::-webkit-scrollbar {
  width: 10px;
}
.hot-list::-webkit-scrollbar-track {
  background: none;
  border-radius: 5px;
}
.hot-list::-webkit-scrollbar-thumb {
  background: var(--ml-color);
  border-radius: 5px;
}
.hot-list::-webkit-scrollbar-thumb:hover {
  background: var(--mh-color);
}
</style>
